<script setup>
import { useRouter, useRoute, onBeforeRouteUpdate } from 'vue-router';
import { useUserStore } from '@/stores';
import { computed, ref } from 'vue'
const userStore = useUserStore()
const router = useRouter()
const route = useRoute()
// 默认选中
const defaultActive = ref(route.path)
// 监听路有变化
onBeforeRouteUpdate((to, from) => {
  console.log(to, from);
  defaultActive.value = to.path
})
// /是否折叠
const isCollapse = computed(() => {
  return !(userStore.asideWidth === '250px')
})
const asideMenus = computed(() => userStore.menusList)
const handleSelect = (e) => {
  console.log(e);
  router.push(e)
}
</script>
<template>
  <div class="f-menu" :style="{ width: userStore.asideWidth }">
    <el-menu unique-opened :collapse-transition="false" :collapse="isCollapse" :default-active="defaultActive"
      default-active="2" class="border-0" @select="handleSelect">
      <template v-for="(item, index) in asideMenus" :key="index">
        <el-sub-menu v-if="item.child && item.child.length > 0" :index="item.name">
          <template #title>
            <el-icon>
              <component :is="item.icon"></component>
            </el-icon>
            <span>{{ item.name }}</span>
          </template>
          <el-menu-item v-for="(item2, index2) in item.child" :key="index2" :index="item2.frontpath">
            <el-icon>
              <component :is="item2.icon"></component>
            </el-icon>
            {{ item2.name }}
          </el-menu-item>
        </el-sub-menu>
        <el-menu-item v-else :index="item.frontpath">
          <el-icon>
            <component :is="item.icon"></component>
          </el-icon>
          <span>{{ item.name }}</span>
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>

<style scoped>
.f-menu {
  transition: all 0.2s;
  top: 64px;
  bottom: 0;
  left: 0;
  overflow-y: auto;
  overflow-x: hidden;
  @apply shadow-md fixed;
}

/* 设置滚动条 */
.f-menu::-webkit-scrollbar {
  width: 0;
}
</style>